package bezier;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class BezierView extends View {
    private final String TAG=getClass().getSimpleName();
    private float width,height;
    public BezierView(Context context) {
        super(context);
    }

    public BezierView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BezierView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * View的宽高
     * @param widthMeasureSpec  宽度
     * @param heightMeasureSpec 高度
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=MeasureSpec.getSize(widthMeasureSpec);
        height=MeasureSpec.getSize(heightMeasureSpec);
        Log.d(TAG,"width="+widthMeasureSpec+",height="+height+",1/3height="+height/3+"2/3height="+height*2/3);
    }

    /**
     * 画笔
     * @param canvas 画布
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint=new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);//中空画笔
        paint.setColor(Color.BLACK);
        //在画布上画一条线
        canvas.drawLine(0,height/3,width,height/3,paint);
        canvas.drawLine(0,height*2/3,width,height*2/3,paint);

        paint.setColor(Color.RED);
        paint.setStrokeWidth(10);//画笔的宽度
        //绘制一阶贝塞尔曲线 lineTo
//        canvas.drawLine(100,100,900,400,paint);
        Path path=new Path();
        path.moveTo(100,100);
        path.lineTo(900,400);
        canvas.drawPath(path,paint);

        //绘制二阶贝塞尔曲线 quadTo
        paint.setColor(Color.GREEN);
        Path path2=new Path();
        path2.moveTo(100,800);//起点
        path2.quadTo(300,560,width-50,800);//支点1，终点
        canvas.drawPath(path2,paint);
        canvas.drawCircle(300,560,5,paint);//支点
        //绘制3阶贝塞尔曲线 cubicTo
        paint.setColor(Color.BLUE);
        Path path3=new Path();
        path3.moveTo(100,1300);
        path3.cubicTo(300,1400,900,1800,width-100,1600);
        canvas.drawPath(path3,paint);
        canvas.drawCircle(300,1400,5,paint);
        canvas.drawCircle(900,1800,5,paint);


    }
}
